package com.weipai.springboot.module.service.impl;

import com.sun.org.apache.regexp.internal.RE;
import com.weipai.springboot.enums.RetCode;
import com.weipai.springboot.model.*;
import com.weipai.springboot.model.api.PageResult;
import com.weipai.springboot.model.api.Response;
import com.weipai.springboot.module.dao.SpecialOfferDao;
import com.weipai.springboot.module.mapper.SpecialOfferMapper;
import com.weipai.springboot.module.service.SpecialOfferService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 曹朋飞
 * @Date: 2018/08/04 10:12
 * 
 * 活动会场
 */
@Service
public class SpecialOfferServiceImpl implements SpecialOfferService{

    private static final Logger log = LoggerFactory.getLogger(SpecialOfferServiceImpl.class);
    
    @Autowired
    private SpecialOfferDao specialOfferDao;
    
    //保存
    @Override
    public Response save(SpecialOffer specialOffer) {
        Response response = new Response();
        if(1==specialOfferDao.insert(specialOffer)){
            response.setCode(RetCode.SUCCESS);
        }else {
            log.debug("保存会场失败:{}",specialOffer.getSpecialOfferTheme());
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }

    //查询未删除（删除）的数据
    @Override
    public List<SpecialOffer> selectAllByDel(int del) {
        return specialOfferDao.selectAllByDel(del);
    }

    //修改
    @Override
    public Response update(SpecialOffer specialOffer) {
        Response response = new Response();
        if(1==specialOfferDao.updateByPrimaryKey(specialOffer)){
            response.setCode(RetCode.SUCCESS);
        }else{
            log.debug("修改会场失败:{}",specialOffer.getSpecialOfferTheme());
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }

    //查询会场详情
    @Override
    public Response getSpecialOfferById(String specialOfferId) {
        Response response = new Response();
        SpecialOffer specialOffer = specialOfferDao.getSpecialOfferById(specialOfferId);
        if(null!=specialOffer){
            response.setCode(RetCode.SUCCESS);
            response.put("data", specialOffer);
        }else {
            response.setCode(RetCode.NO_DATA);
        }
        return response;
    }

    //删除
    @Override
    public Response deleteById(String specialOfferId) {
        Response response = new Response();
        if(1==specialOfferDao.updateDel(specialOfferId)){
            response.setCode(RetCode.SUCCESS);
        }else {
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }

    //更改会场活动的状态
    @Override
    public Response changeStatus(String specialOfferId, Integer status) {
        Response response = new Response();
        if(1==specialOfferDao.updateStatus(specialOfferId,status)){
            response.setCode(RetCode.SUCCESS);
        }else {
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }

    //批量删除
    @Override
    public Response batchDeleteById(String[] id) {
        Response response = new Response();
        if(specialOfferDao.batchDeleteById(id)>0){
            response.setCode(RetCode.SUCCESS);
        }else {
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }

    //分页查询
    @Override
    public Response selectAllByPage(Integer page, Integer size, String draw, Integer type) {
        Integer start = (page-1) * size;
        List<SpecialOffer> specialOfferList = specialOfferDao.selectAllByPage(start, size, type);
        Integer totalNum = specialOfferDao.selectAllByPageAmount(type);
        PageResult<SpecialOffer> pageList = new PageResult<>(specialOfferList,page,size,totalNum, draw);
        Response response = new Response();
        response.setCode(RetCode.SUCCESS);
        response.put("pageList", pageList);
        return response;
    }

    // APP  首页 获取 举办会场详情
    @Override
    public Response getSpecialOffer() {
        Response response = new Response();
        List<SpecialOffer> specialOfferList = specialOfferDao.getSpecialOffer();

        if(specialOfferList.size()>0){
            SpecialOffer specialOffer = specialOfferList.get(0);
            response.put("pageList", specialOffer);
            response.setCode(RetCode.SUCCESS);
        }else{
            response.setCode(RetCode.NO_DATA);
        }
        return response;
    }
}
